<?php
//addslashes() 函数在指定的预定义字符前添加反斜杠
function daddslashes($string) {
    $string=str_replace("'",'"',$string);
    !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
    if(!MAGIC_QUOTES_GPC) {
        if(is_array($string)) {
            foreach($string as $key => $val) {
                $string[$key] = daddslashes($val);
            }
        } else {
            $string = addslashes($string);
        }
    }
	return $string;
}
//词语过滤
function replace($content){
    global $replace;
    $content=clean_html($content);
    if($content=="") {
        $content="<FONT COLOR=\"red\">***HTML代码已经过滤***</font>";
    }
    $bad = explode("|",$replace);
    @reset($bad);
    for ($i=0;$i<count($bad);$i++)
	$content= str_replace($bad[$i],"<FONT COLOR=\"red\">***</font>",$content);
    return $content;
}

//过滤html
function clean_html($html)
{
    $html =eregi_replace('<("|\')?([^ "\']*)("|\')?.*>([^<]*)<([^<]*)>', '\4', $html);
    $html = preg_replace('#</?.*?\>(.*?)</?.*?\>#i','',$html);
    $html = preg_replace('#(.*?)\[(.*?)\](.*?)javascript(.*?);(.*?)\[/(.*?)\](.*?)#','', $html);
    $html = preg_replace('#javascript(.*?)\;#','', $html);
    $html = str_replace("<br />", "", $html);
    $html = htmlspecialchars($html);
    return($html);
}

//链接过滤
function apiurlreplace($content) {
    $cbody=preg_replace("/\[(quote|b|u|i|s|colour=.*|url|url=.*|img.*|strike|size=.*)\](\S+?)\[\/.*\]/i","$2",$content);
    $cbody =eregi_replace('(.+)我在相册上传了一张照片(.+)', '我在相册上传了一张照片\2', $cbody);
    return $cbody;
}
function StrLenW($str){
    $len=strlen($str);
    $i=0;
    while($i<$len){
        if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str[$i])){
            $i+=2;
        }else{
            $i+=1;
        }
    }
    return $i;
}
function CheckEmail($C_mailaddr) 
{ 
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$", $C_mailaddr)) 
return false; 
return true; 
} 
function CheckUser($C_user) 
{ 
if (!ereg("^[_a-zA-Z0-9]*$", $C_user)) return false; //特殊字符检验 
return true; 
} 

//加解密
function DeCode($string,$operation,$key='')
    {
        $key=md5($key);
        $key_length=strlen($key);
        $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;
        $string_length=strlen($string);
        $rndkey=$box=array();
        $result='';
        for($i=0;$i<=255;$i++)
        {
            $rndkey[$i]=ord($key[$i%$key_length]);
            $box[$i]=$i;
        }
        for($j=$i=0;$i<256;$i++)
        {
            $j=($j+$box[$i]+$rndkey[$i])%256;
            $tmp=$box[$i];
            $box[$i]=$box[$j];
            $box[$j]=$tmp;
        }
        for($a=$j=$i=0;$i<$string_length;$i++)
        {
            $a=($a+1)%256;
            $j=($j+$box[$a])%256;
            $tmp=$box[$a];
            $box[$a]=$box[$j];
            $box[$j]=$tmp;
            $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
        }
        if($operation=='D')
        {
            if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8))
            {
                return substr($result,8);
            }
            else
            {
                return'';
            }
        }
        else
        {
            return str_replace('=','',base64_encode($result));
        }
    }
//随机字符串
function getRandchar(){
$str = "0123456789abcdefghijklmnopqrstuvwxyz";
$n = 8;
$len = strlen($str)-1;
for($j=0;$j<2;$j++)
	{
		for($i=0;$i<$n;$i++)
		{
			$s.= $str[rand(0,$len)];
		}
	}
	return $s;
}
function SendMail($active)
{
	$file=fopen("test.txt",'a');
	fwrite($file,$active);
	fclose($file);
}
function esetcookie($name,$value,$life = 0, $prefix = 1)
{
	global $cookiedomain, $cookiepath, $addtime, $_SERVER;
	setcookie($name, $value,$life ? $addtime + $life : 0, $cookiepath,$cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}
?>